<?php

namespace App\Http\Controllers\Admin\CMS;

use App\Http\Models\Admin\CMS\Category;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Psy\Util\Json;


class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        //
        return View::make('admin.cms.category')->withUserinfo($request->session()->get('adminUser'))
            ->withUrltitle(trans("admin/message.category_index"))
            ->withAssets($request['ASSETS']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $list = Category::whereNull('parent_id')->orWhere('parent_id',0)->orderBy('show_index','asc')->get();
        foreach($list as $item){
            $this->inTreeData($item);
        }
        return Json::encode($list);
    }

    public function inTreeData($data)
    {
        //
        $data->children =  Category::where('parent_id',$data->id)->orderBy('show_index','desc')->get();

        if($data->children == null){
            return;
        }
        foreach($data->children as $child){
            $this->inTreeData($child);
        }

    }
    public function updateCtrees(Request $request)
    {
        $data_src = $request->input("data");
        $datas = json_decode($data_src);
        Category::whereNotNull('id')->update(['parent_id'=>0]);

        foreach($datas as $k=>$data){
            Category::where('id', $data->id)->update(['parent_id'=>0,'show_index'=>$k]);
            $this->updateCtreesItem($data);
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
    public function updateCtreesItem($item)
    {
        if(property_exists($item,'children')) {
            foreach($item->children as $k=>$citem){
                Category::where('id', $citem->id)->update(['parent_id'=>$item->id,'show_index'=>$k]);
                $this->updateCtreesItem($citem);
            }
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    public function store(Request $request)
    {

        $validator = Validator::make(Input::all(),
            [
                'name' => 'required',
            ]);


        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{



            $category = new Category();
            $category->name = $request->input("name");
            $category->desc = $request->input("desc");
            if("" !== $request->input("parent_id")){
                $category->parent_id = $request->input("parent_id");
            }

            if($request->input("id")){
                //更新操作
                $exit_item = Category::where('id','<>',$request->input("id"))->where('name',$category->name)->first();
                if($exit_item){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                else{
                    $up_item = Category::find($request->input("id"));
                    $up_item->name = $category->name;
                    $up_item->desc = $category->desc;
                    $up_item->parent_id = $category->parent_id;
                    $up_item->save();
                    return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
                }
            }
            else{
                //添加操作
                $find = Category::where('name', $category->name)->first();
                if($find){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                $category->save();
                return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
            }




        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $category = Category::find($id);
        if($category){
            $parent = Category::find($category->parent_id);
            if($parent){
                $category->parent_name = $parent->name;
            }

        }
        return Json::encode($category);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
